package org.rainbowinverted.charging.device.connect.bl.server.handler;

import cn.hutool.core.net.Ipv4Util;
import com.dix.codec.bkv.BKV;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.rainbowinverted.charging.device.connect.bl.server.message.out.BlDeviceUpdateFtpOut;
import org.springframework.stereotype.Component;

/**
 * bl ftp 更新处理器
 * @author huaiping
 */
@Component
@ChannelHandler.Sharable
public class BlDeviceUpdateFtpHandler extends BlAbstractHandler<Boolean, BlDeviceUpdateFtpOut>{
    @Override
    public void write(BlDeviceUpdateFtpOut message, BKV bkv) {
        String ipHexStr = String.format("%08x", Ipv4Util.ipv4ToLong(message.getFtpIp()));
        bkv.add(0x19, ipHexStr);
        bkv.add(0x1a, String.format("%04x", message.getFtpPort()));
        bkv.add(0x1b, message.getOtaFileName());
        createFuture(getSerialNo(bkv));
    }

    @Override
    public void read(ChannelHandlerContext channelHandlerContext, BKV bkv) {
        byte ack = getAck(bkv);
        complete(getSerialNo(bkv), ack == 0x01);
    }
}
